package com.ilop.sthome.mvp.present.scene;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.example.common.mvp.BasePresenterImpl;
import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.enums.DeviceTrigger;
import com.ilop.sthome.data.enums.SmartDevice;
import com.ilop.sthome.data.greenDao.AutomationBean;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.mvp.contract.scene.ChooseActionContract;
import com.ilop.sthome.ui.activity.scene.AutomationDetailActivity;
import com.ilop.sthome.ui.activity.scene.ChooseActionActivity;
import com.ilop.sthome.ui.activity.scene.ChooseHumitureActivity;
import com.ilop.sthome.ui.activity.scene.SettingActionActivity;
import com.ilop.sthome.ui.activity.scene.SettingTempControlActivity;
import com.ilop.sthome.ui.activity.scene.SettingTimingActivity;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author skygge
 * @date 2019/10/10.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：选择触发条件
 */
public class ChooseActionPresenter extends BasePresenterImpl<ChooseActionContract.IView> implements ChooseActionContract.IPresent{

    private static final String TAG = ChooseActionPresenter.class.getSimpleName();
    private Context mContext;
    private String mDeviceName;
    private List<DeviceInfoBean> fictitiousList;
    private List<DeviceInfoBean> deviceList;

    public ChooseActionPresenter(Context mContext, String mDeviceName) {
        this.mContext = mContext;
        this.mDeviceName = mDeviceName;
        fictitiousList = new ArrayList<>();
        deviceList = new ArrayList<>();
    }

    @Override
    public void getDeviceInList(String deviceName) {
        DeviceInfoBean device = new DeviceInfoBean();
        device.setDevice_type("TIMER");
        device.setDeviceName(deviceName);
        fictitiousList.add(device);
        DeviceInfoBean device2 = new DeviceInfoBean();
        device2.setDevice_type("CLICK");
        device2.setDevice_status("10");
        device2.setDeviceName(deviceName);
        fictitiousList.add(device2);
        mView.getFictitiousList(fictitiousList);

        deviceList.addAll(DeviceDaoUtil.getInstance().findInputDevice(mDeviceName));
        if (deviceList.size()>0){
            mView.getDeviceList(deviceList);
        }else {
            mView.withoutData();
        }
    }

    @Override
    public void getDeviceOutList(String deviceName) {
        DeviceInfoBean device = new DeviceInfoBean();
        device.setDeviceName(deviceName);
        device.setDevice_type("PHONE");
        device.setDevice_status("82FFFF");
        fictitiousList.add(device);
        mView.getFictitiousList(fictitiousList);

        DeviceInfoBean gateway = DeviceDaoUtil.getInstance().findGatewayByDeviceName(mDeviceName);
        deviceList.add(gateway);
        List<DeviceInfoBean> subDevice = DeviceDaoUtil.getInstance().findOutputDevice(mDeviceName);
        deviceList.addAll(subDevice);

        if (deviceList.size()>0){
            mView.getDeviceList(deviceList);
        }else {
            mView.withoutData();
        }
    }

    @Override
    public void onItemClick(DeviceInfoBean device, AutomationBean auto, boolean isAdd, boolean isInput) {
        Intent intent = new Intent();
        intent.putExtra(CommonId.KEY_ADD_NEW, isAdd);
        intent.putExtra(CommonId.KEY_INPUT, isInput);
        intent.putExtra(CommonId.KEY_DEVICE, device);
        intent.putExtra(CommonId.KEY_AUTOMATION, auto);
        if(DeviceTrigger.getType(device.getDevice_type())!=null||!TextUtils.isEmpty(device.getProductKey())){
            intent.setClass(mContext, SettingActionActivity.class);
            mView.startActivityByIntent(intent);
        }else if(SmartDevice.EE_DEV_TH_CHECK == SmartDevice.getType(device.getDevice_type())){
            intent.setClass(mContext, ChooseHumitureActivity.class);
            mView.startActivityByIntent(intent);
        }else if(SmartDevice.EE_TEMP_CONTROL == SmartDevice.getType(device.getDevice_type())){
            intent.setClass(mContext, SettingTempControlActivity.class);
            mView.startActivityByIntent(intent);
        }else if(SmartDevice.EE_DEV_BUTTON == SmartDevice.getType(device.getDevice_type())){
            device.setDevice_status("00");
            if (isAdd){
                List<DeviceInfoBean> deviceInfo = new ArrayList<>();
                deviceInfo.add(device);
                addNext(device.getDeviceName(), deviceInfo);
            }else {
                LiveDataBus.get().with(CommonId.INPUT_CONDITION).setValue(device);
                mView.finishActivity();
            }
        }else if ("TIMER".equals(device.getDevice_type())){
            intent.setClass(mContext, SettingTimingActivity.class);
            mView.startActivityByIntent(intent);
        }else if ("CLICK".equals(device.getDevice_type())){
            if (isAdd){
                List<DeviceInfoBean> deviceInfo = new ArrayList<>();
                deviceInfo.add(device);
                addNext(device.getDeviceName(), deviceInfo);
            }else {
                LiveDataBus.get().with(CommonId.INPUT_CONDITION).setValue(device);
                mView.finishActivity();
            }
        }else if ("PHONE".equals(device.getDevice_type())){
            if (isAdd){
                List<DeviceInfoBean> deviceInfo = new ArrayList<>();
                deviceInfo.add(device);
                addFinish(device.getDeviceName(), auto, deviceInfo);
            }else {
                LiveDataBus.get().with(CommonId.OUTPUT_CONDITION).setValue(device);
                mView.finishActivity();
            }
        }
    }

    private void addNext(String deviceName, List<DeviceInfoBean> list){
        AutomationBean mAuto = new AutomationBean();
        mAuto.setDeviceName(deviceName);
        mAuto.setInputList(list);
        Intent intent2 = new Intent(mContext, ChooseActionActivity.class);
        intent2.putExtra(CommonId.KEY_ADD_SCENE, true);
        intent2.putExtra(CommonId.KEY_INPUT, false);
        intent2.putExtra(CommonId.KEY_DEVICE_NAME, deviceName);
        intent2.putExtra(CommonId.KEY_AUTOMATION, mAuto);
        mView.startActivityByIntent(intent2);
    }

    private void addFinish(String deviceName, AutomationBean bean, List<DeviceInfoBean> list){
        bean.setOutputList(list);
        Intent intent = new Intent();
        intent.setClass(mContext, AutomationDetailActivity.class);
        intent.putExtra(CommonId.KEY_ADD_SCENE, true);
        intent.putExtra(CommonId.KEY_AUTOMATION, bean);
        intent.putExtra(CommonId.KEY_DEVICE_NAME, deviceName);
        mView.startActivityByIntent(intent);
    }
}
